// haojk/pages/apply-vip/index.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    applyleader_fee:0,
    applyleader_remark:"支付会员以后即可成为盟主",
    orderno:"",
    prepay_id:"",
    from_id:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var fee = wx.getStorageSync("applyleader_fee");
    var globalset = wx.getStorageSync("globalset");
    this.setData({
      applyleader_fee: fee
    });
    if (globalset.applyleader_remark!=""){
      this.setData({
        applyleader_remark: globalset.applyleader_remark
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }, pay(e) {
    const self = this;
    var applyleader_fee=self.data.applyleader_fee;
  
    self.setData({ "from_id": e.detail.formId });
    if (applyleader_fee > 0) {
      wx.showModal({
        title: '确认支付',
        content: '确认支付' + applyleader_fee + '元成为盟主吗？',
        success: function (res) {
          
          if (res.confirm) {
          
            app.util.request({
              url: 'entry/wxapp/pay',
              data: {
                "m": 'nets_haojk'
              },
              showLoading: true,
              method: 'post',
              success(res) {
               
              
                self.setData({ "orderno": res.data.data.tid });
                if (res.data && res.data.data) {
                  self.setData({"prepay_id":res.data.data.package});
                  wx.requestPayment({
                    'timeStamp': res.data.data.timeStamp,
                    'nonceStr': res.data.data.nonceStr,
                    'package': res.data.data.package,
                    'signType': 'MD5',
                    'paySign': res.data.data.paySign,
                    'success': function (res) {
                      
                      self.getMember();
                      //支付成功后，系统将会调用payResult() 方法，此处不做支付成功验证，只负责提示用户
                    },
                    'fail': function (res) {
                     
                      //支付失败后，
                    }
                  });
                }
              }
            })
          }
        }
      })
    } else {
      this.setData({
        showTopTips: true,
        tip: '金额不合法'
      });
      setTimeout(function () {
        self.setData({
          showTopTips: false
        });
      }, 3000);
    }
  },
  getMember: function (res) {
    var that = this;
    app.getUserInfo(function (userInfo) {
      //更新数据
      app.util.getUserInfo(function (response) {
        wx.showToast({
          title: '登陆成功'
        });
        response.memberInfo.nickname = userInfo.nickName;
        response.memberInfo.avatar = userInfo.avatarUrl;
        response.memberInfo.city = userInfo.city;
        response.memberInfo.country = userInfo.country;
        response.memberInfo.province = userInfo.province;
        response.memberInfo.gender = userInfo.gender;
        that.addMember(response.memberInfo);
      });
    });
    // 页面渲染完成
  },
  addMember: function (userInfo) {
    var $this = this;
   
    app.util.request({
      url: 'entry/wxapp/Result',
      data: {
        "m": 'nets_haojk',
        "orderno":$this.data.orderno,
        "prepay_id": $this.data.prepay_id,
        "from_id": $this.data.from_id,
        "money": $this.data.applyleader_fee
      },
      method: 'post',
      success: function (response) {
        //首页存储会员对象到缓存，其他页面直接读取缓存即可
      
        /* 走支付通知完成支付了，这里不用了
        if (response.data.data==""){
          wx.showModal({
            title: '提示',
            content: response.data.message,
            success: function (res) {
              if (res.confirm) {
                wx.reLaunch({
                  url: "../my/index"
                });
              } else if (res.cancel) {
                wx.reLaunch({
                  url: "../my/index"
                });
              }
            }
          });
       
        }
        */
        wx.setStorage({
          key: "memberinfo",
          data: response.data.data
        });
        $this.setData({
          memberid: response.data.data.memberid,
          datalock: false
        });
        wx.showModal({
          title: '提示',
          content: '支付成功即将返回',
          success: function (res) {
            if (res.confirm) {
              wx.reLaunch({
                url: "../my/index"
              });
            } else if (res.cancel) {
              wx.reLaunch({
                url: "../my/index"
              });
            }
          }
        });
        
        //取会员对象的方式
        wx.getStorage({
          key: 'memberinfo',
          success: function (res) {
          }
        })
      }
    });
  }
})